package xkp.lesson.store.activity.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import xkp.lesson.store.Config;
import xkp.lesson.store.R;
import xkp.lesson.store.adapter.BuildAdapter;
import xkp.lesson.store.entity.Building;
import xkp.lesson.store.entity.JsonData;
import xkp.lesson.store.utils.HttpUtils;


public class BuildListFragment extends Fragment {

    private RecyclerView buildListView;
    private BuildAdapter adapter;
    private OkHttpClient okHttpClient;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_buildlist, container, false);
        buildListView = root.findViewById(R.id.buildListView);
        buildListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        buildListView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));

        return root;
    }

    @Override
    public void onStart() {
        super.onStart();
        loadData();
    }

    private void loadData() {
        String url = Config.BASE_URL + "/building/listbyUserId";
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .addHeader("token", Config.token)
                .addHeader("mobile", "mobile")
                .build();
        okHttpClient = HttpUtils.getClient();
        //mOkHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.e("str",str);
                final JsonData<List<Building>> jsonData = JSON.parseObject(str, new TypeReference<JsonData<List<Building>>>() {
                });

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new BuildAdapter(getActivity(), jsonData.getData());
                        buildListView.setAdapter(adapter);
                    }
                });
            }

        });
    }
}
